const util = require('./util.js')
const baseurl = util.baseurl
export { getActivityList, addActivity, updateActivity, getActivityDetail };
//获取活动列表
const getActivityList = () => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Activity/GetActivityList',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//获取活动详情
const getActivityDetail = (id) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Activity/GetActivityById',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                'id': id
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//添加活动
const addActivity = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Activity/EnrollActivity',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                'Theme': data.title, //主题
                'ResponsiblePerson': data.leader,
                'ActivityStartTime': data.startTime,
                'ActivityEndTime': data.endTime,
                'ActivityAddress': data.location,
                'Discription': data.description,
                'Participants': data.participants,
                'Situation': data.status
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
//更新活动
const updateActivity = (data) => {
    return new Promise((resolve, reject) => {
        const userInfo = wx.getStorageSync('userInfo')
        // console.log(userInfo)
        wx.request({
            url: baseurl + '/Activity/UpdateActivity',
            method: 'POST',
            header: {
                'content-type': 'application/json',
                'Username': userInfo.username,
                'Authorization': userInfo.token,
                'Userrole': userInfo.role,
                'Uid': userInfo.userid
            },
            data: {
                'Theme': data.title, //主题
                'ResponsiblePerson': data.leader,
                'ActivityStartTime': data.startTime,
                'ActivityEndTime': data.endTime,
                'ActivityAddress': data.location,
                'Discription': data.description,
                'Participants': data.participants,
                'Situation': data.status
            },
            success: (res) => {
                // console.log(res)
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
